package com.anzia.set;

import java.util.HashSet;
import java.util.Iterator;

/**
 *@ClassName HashSet_Demo
 *@Description
 *@Author Anzia.
 *@Date 2021/4/10 13:38
 *@Version 1.0
 **/
public class HashSet_Demo {
    public static void main(String[] args) {
        //创建集合
        HashSet<String> set = new HashSet<>();

        //添加数据
        set.add("小米");
        set.add("华为");
        set.add("苹果");
        set.add("华为");
        System.out.println("数据个数：" + set.size());
        System.out.println(set.toString());

        //删除数据
        //set.remove("小米");
        //set.clear();
        //System.out.println(set);

        //遍历
        //使用增强for循环
        System.out.println("-----------");
        for (String s : set) {
            System.out.println(s);
        }
        //使用迭代器
        System.out.println("-----------");
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        //判断
        System.out.println("-----------");
        System.out.println(set.contains("华为"));
        System.out.println(set.isEmpty());

    }
}
